package com.senmiao.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senmiao.ssm.entity.Employee;
import com.senmiao.ssm.exception.MyException;
import com.senmiao.ssm.mapper.EmployeeMapper;
import com.senmiao.ssm.service.BusinessService;
import com.senmiao.ssm.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Result getEmployeeById(Integer employeeId) {
        Employee employee = getEmployee(employeeId);
        return Result.ok().data("data",employee);
    }

    @Override
    public Result createEmployee(String name, Integer age, Integer sex) {
        Employee employee = new Employee();
        employee.setName(name);
        employee.setAge(age);
        employee.setSex(sex);
        employee.setIsDelete(0);
        employee.setCreateTime(new Date());
        int insert = employeeMapper.insert(employee);
        if (insert < 1){
            throw new MyException("保存到数据库异常");
        }
        return Result.ok();
    }

    @Override
    public Result updateEmployee(Integer employeeId,String name, Integer age, Integer sex) {
        Employee employee = getEmployee(employeeId);
        employee.setName(name);
        employee.setAge(age);
        employee.setSex(sex);
        employee.setModifiedTime(new Date());
        int update = employeeMapper.updateById(employee);
        if (update < 1){
            throw new MyException("更新到数据库异常");
        }
        return Result.ok();
    }

    @Transactional
    @Override
    public Result deleteEmployeeByIds(List<Integer> employeeIds) {
        List<Employee> employees = employeeMapper.selectList(new QueryWrapper<Employee>()
                .in("id", employeeIds)
                .eq("is_delete", 0));
        for (Employee employee : employees) {
            employee.setIsDelete(1);
            employee.setModifiedTime(new Date());
            int update = employeeMapper.updateById(employee);
            if (update < 1){
                throw new MyException("从数据库删除失败");
            }
        }
        return Result.ok();
    }

    @Override
    public Result listEmployee(Integer pageNum, Integer limit) {
        Page<Employee> employeePage = employeeMapper.selectPage(new Page<Employee>(pageNum, limit)
                , new QueryWrapper<Employee>().eq("is_delete", 0));
        return Result.ok().data("list",employeePage.getRecords())
                .data("total",employeePage.getTotal())
                .data("pageNum",employeePage.getCurrent());
    }

    /**
     * 通过员工id获取员工信息
     * @param employeeId
     * @return
     */
    private Employee getEmployee(Integer employeeId) {
        Employee employee = employeeMapper.selectOne(new QueryWrapper<Employee>().eq("id", employeeId).eq("is_delete", 0));
        if (employee == null){
            throw new MyException("员工id不存在");
        }
        return employee;
    }
}
